import { CommandDialog } from '@amcax/base';
import { CommandLabeledItemWrapper } from '@app-cad/common/components/command-form/item-wrapper';
import i18n from '@app-cad/common/utils/i18next';
import { isShapePlaneCopyChild } from '@app-cad/common/utils/plane-copy';
import { CurvedBaseCommand, CurvedCmdDialogParams } from '@app-cad/types';
import { Select } from 'antd';
import { useEffect, useState } from 'react';
import './index.scss';
import { Plane3Points } from './3point';
import { PlaneOffset } from './plane-offset';
import { PlanePoint } from './plane-point';
import { PlaneRotate } from './plane-rotate';
import { PlaneProvider } from './PlaneContext';
import { PlanePointLine } from './point-line';
import { PlaneTwoLine } from './plane-two-line';
import { PlaneCurve } from './plane-curve';
import { PlaneNormalCurve } from './plane-normal-curve';
import { PlaneTangentSurface } from './plane-tangent-surface';
import { PlaneBetween } from './plane-between';

const CommandPlane = ({ title, onClose, props }: CurvedCmdDialogParams) => {
  const dxid = props?.shape;
  const isPlaneCopyChild = isShapePlaneCopyChild(dxid);

  const [mode, setMode] = useState<
    | 'translate'
    | 'point-tr'
    | 'between-planes'
    | 'rotate'
    | '3point'
    | 'point-line'
    | '2lines'
    | 'planeCurve'
    | 'normalCurve'
    | 'tangentSurface'
  >('translate');

  // props需要为state，以确保props和mode同时更新，分发的模式不会收到错误的props
  const [editProps, setEditProps] = useState<any>();
  const [editDxid, setEditDxid] = useState<string>();

  useEffect(() => {
    setMode(props?.params?.mode ?? 'translate');
    setEditProps(props);
    setEditDxid(dxid);
  }, [dxid]);

  return (
    <CommandDialog
      title={title}
      onClose={onClose}
      placement='topLeft'
    >
      <PlaneProvider>
        <CommandLabeledItemWrapper
          className='mt-5 px-5'
          label='平面类型'
        >
          <Select
            options={[
              { value: 'translate', label: '偏移平面' },
              { value: 'point-tr', label: '平行通过点' },
              { value: 'between-planes', label: '之间' },
              { value: 'rotate', label: '旋转平面' },
              { value: '3point', label: '通过三个点' },
              { value: 'point-line', label: '通过点和直线' },
              { value: '2lines', label: '通过两条直线' },
              { value: 'planeCurve', label: '通过平面曲线' },
              { value: 'normalCurve', label: '曲线的法平面' },
              { value: 'tangentSurface', label: '曲面的切平面' },
            ]}
            value={mode}
            onChange={(val) => {
              if (val !== mode) {
                setMode(val);
                setEditProps(null);
              }
            }}
            disabled={isPlaneCopyChild}
          />
        </CommandLabeledItemWrapper>
        {mode === 'translate' && (
          <PlaneOffset
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
            readonly={isPlaneCopyChild}
          />
        )}
        {mode === 'point-tr' && (
          <PlanePoint
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
          />
        )}
        {mode === 'between-planes' && (
          <PlaneBetween
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
          />
        )}
        {mode === 'rotate' && (
          <PlaneRotate
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
            readonly={isPlaneCopyChild}
          />
        )}
        {mode === '3point' && (
          <Plane3Points
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
          />
        )}
        {mode === 'point-line' && (
          <PlanePointLine
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
          />
        )}
        {mode === '2lines' && (
          <PlaneTwoLine
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
          />
        )}
        {mode === 'planeCurve' && (
          <PlaneCurve
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
          />
        )}
        {mode === 'normalCurve' && (
          <PlaneNormalCurve
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
          />
        )}
        {mode === 'tangentSurface' && (
          <PlaneTangentSurface
            props={editProps}
            onClose={onClose}
            editDxid={editDxid}
          />
        )}
      </PlaneProvider>
    </CommandDialog>
  );
};

export const cmdPlane: CurvedBaseCommand = {
  id: 'CmdPlane',
  label: 'command.plane',
  icon: 'commands/curved/plane',
  create: (label, onClose, props) => (
    <CommandPlane
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
